Repository navigation
[REBASE & FF] Finish Unmapping Free Heap Pages - #1916
Conversation
✅ QEMU Validation PassedAll QEMU validation jobs completed successfully.
Workflow run: https://github.com/OpenDevicePartnership/patina/actions/runs/41891403071 Boot Time to EFI Shell
Dependencies
Binary Size Report
Compressed sizes use LZMA-alone format at preset 6 ( Shared QEMU integration: This comment was automatically generated by the Patina QEMU PR Validation Post workflow. |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
|
I did do some profiling for the large pool allocations and on Q35, there were ~1,300 allocations >4K <8k and ~60 allocations >8k. I did a perf test on Intel HW with extending the FSBs to 8k. There was no appreciable perf difference in doing so and there was additional overhead since the retired page tracking assumes a max of 1 page at a time, so I dropped that code. |
609252b to
126b649
Compare
|
@makubacki I have addressed feedback, please re-review. |
cfernald
left a comment
There was a problem hiding this comment.
I think we should try further improvements such as lazily splitting the FSB pages, but this change is already pretty large so I don't think now is the time.
Done |
126b649 to
9de42b2
Compare
|
@cfernald @makubacki @joschock I have pushed one more commit to address the issue Chris raised that ARM64 runtime allocators could end up with very large allocations for large heap allocations. This alleviates that problem by searching retired pages for allocations first before doing a direct GCD allocation. Please re-review. The earlier commits are unchanged. I am getting a new run of perf data on both x64 and arm64 with this. |
Today, the fixed size block allocator and the GCD have a circular dependency. The FSB allocates pages from the GCD to fill its fixed block lists and directly sends page allocations to the GCD. The GCD does not allocate its descs from pool backed memory, but does fire the memory map and cache attribute change events, which go into the event DB, which does allocate pool memory. This causes odd locking behavior to be required in the FSB to drop the lock (which also lowers TPL back down to the caller's level, processing pending events), call into the GCD (acquiring its lock, raising TPL to HIGH, then dropping its lock, lowering TPL, processing events), then acquiring the FSB lock again (going to TPL HIGH, then lowering and processing events). Outside of these two events, the GCD does not do any pool allocations. The strange locking behavior leaves gaps for allocs/frees to be incomplete when other events are being processed, which can lead to a bad state, particularly in upcoming changes to the allocator structure. It also has a negative performance impact to do the TPL manipulations and subsequent event processing many times. This commit adds special handling for the memory map and cache attribute change event, preventing memory allocation and deferring their execution until the next TPL lowering, when we process pending events anyways. This will allow a future commit in this series to change the FSB locking to remain at TPL_HIGH_LEVEL and holding the FSB lock for the duration of an alloc/free, removing the perf concern and the reliability gaps. These events are safe to defer and in fact it is better to, further memory map modifications may occur (which may also change cache attributes) and so instead of firing the event multiple times, we fire a single time when the operation is complete. Signed-off-by: Oliver Smith-Denny <osde@microsoft.com>
The logic of when to send a cache event and call the cacheability change API is a bit hard to read, this cleans it up. Signed-off-by: Oliver Smith-Denny <osde@microsoft.com>
The GCD currently finds a candidate memory block for an allocation, splits the block, does list maintenance, checks if the block is allocatable, then if it is not, it remerges the block, does list maintenance, and returns an error, telling the GCD to try the next block. This has been observed to have a large performance impact due to this being on the memory allocation hotpath. By moving the logic to check if the block is allocatable before doing the split, a physical Intel platform was seen to boot >200ms faster. Before this change, on the platform tested, each GCD allocation was observed to have an average of 54 blocks split and remerged before it found a block that was actually allocatable. This equated to 2.8 billion ticks spent doing splits and merges vs 200 million ticks spent doing splits and merges after the change. This commit changes the bottom up and top down allocations to check if the block is allocatable before splitting it. Signed-off-by: Oliver Smith-Denny <osde@microsoft.com>
Currently, we use the linked list allocator crate to manage allocations that don't hit our fixed size blocks. However, our use of the crate is fairly minimal and it prevents us from unmapping the pages that the allocator owns, which is contrary to our use after free catching goals. This commit does a large refactor to the allocator system to stop using the linked list allocator crate and instead back all heap allocations greater than our largest FSB with pages directly. It then updates the management of retired and reclaimed pages to account for the new management. To keep this management simple, when a page is reclaimed, it is eagerly split into one block size. Following this change, all unused heap pages are unmapped, except for the pages that keep metadata on this heap region. Heap pages are retired and reclaimed in FIFO order (as are FSBs) to have the longest time between using a page and it getting used again and increasing the change that page will be retired and catch any use after free. Signed-off-by: Oliver Smith-Denny <osde@microsoft.com>
An earlier refactor the FSB introduced the page accounting node as a last used cache for heap owned regions as a potential performance improvement. However, the performance of just this piece was not tested independently. When testing independently on physical HW, it was not observed to add any performance benefit, so it is dropped. Signed-off-by: Oliver Smith-Denny <osde@microsoft.com>
In order to minimize the runtime memory footprint of large heap allocations (greater than one page) for architectures with large runtime allocation granularity, e.g. 64KB for ARM64, this changes the FSB to have a new allocation tier that will attempt allocations between 4KB and 64KB from the retired pages it holds instead of directly from the GCD. Allocations larger than 64KB will still be directly allocated from the GCD. Signed-off-by: Oliver Smith-Denny <osde@microsoft.com>
9de42b2 to
f340ad7
Compare
CI started enforcing a new lint on changed files and this was flagged for using the incorrect unordered list style. Signed-off-by: Oliver Smith-Denny <osde@microsoft.com>
Description
This PR is the completion of the work started in #1837. It removes the linked list allocator crate in favor of direct GCD allocations when the fixed size blocks cannot satisfy a request ensures that all heap pages are unmapped when not in use (allocated by callers or used as metadata pages).
Closes #603 and #1846.
There are five commits:
EventDB: Defer Memory Map and Cache Attribute Change Events
Today, the fixed size block allocator and the GCD have a circular dependency. The FSB allocates pages from the GCD to fill its fixed block lists and directly sends page allocations to the GCD. The GCD does not allocate its descs from pool backed memory, but does fire the memory map and cache attribute change events, which go into the event DB, which does allocate pool memory. This causes odd locking behavior to be required in the FSB to drop the lock (which also lowers TPL back down to the caller's level, processing pending events), call into the GCD (acquiring its lock, raising TPL to HIGH, then dropping its lock, lowering TPL, processing events), then acquiring the FSB lock again (going to TPL HIGH, then lowering and processing events).
Outside of these two events, the GCD does not do any pool allocations. The strange locking behavior leaves gaps for allocs/frees to be incomplete when other events are being processed, which can lead to a bad state, particularly in upcoming changes to the allocator structure. It also has a negative performance impact to do the TPL manipulations and subsequent event processing many times.
This commit adds special handling for the memory map and cache attribute change event, preventing memory allocation and deferring their execution until the next TPL lowering, when we process pending events anyways. This will allow a future commit in this series to change the FSB locking to remain at TPL_HIGH_LEVEL and holding the FSB lock for the duration of an alloc/free, removing the perf concern and the reliability gaps.
These events are safe to defer and in fact it is better to, further memory map modifications may occur (which may also change cache attributes) and so instead of firing the event multiple times, we fire a single time when the operation is complete.
GCD: Cleanup Cache Event Sending
The logic of when to send a cache event and call the cacheability change API is a bit hard to read, this cleans it up.
GCD: Check Allocation Suitability Before Splitting Block
The GCD currently finds a candidate memory block for an allocation, splits the block, does list maintenance, checks if the block is allocatable, then if it is not, it remerges the block, does list maintenance, and returns an error, telling the GCD to try the next block.
This has been observed to have a large performance impact due to this being on the memory allocation hotpath. By moving the
logic to check if the block is allocatable before doing the split, a physical Intel platform was seen to boot >200ms faster. Before this change, on the platform tested, each GCD allocation was observed to have an average of 54 blocks split and remerged
before it found a block that was actually allocatable. This equated to 2.8 billion ticks spent doing splits and merges vs 200 million ticks spent doing splits and merges after the change.
This commit changes the bottom up and top down allocations to check if the block is allocatable before splitting it.
Allocator: Unmap Non-FSB Heap Pages and Drop Allocator Crate
Currently, we use the linked list allocator crate to manage allocations that don't hit our fixed size blocks. However, our use of the crate is fairly minimal and it prevents us from unmapping the pages that the allocator owns, which is contrary to our use after free catching goals.
This commit does a large refactor to the allocator system to stop using the linked list allocator crate and instead back all heap allocations greater than our largest FSB with pages directly. It then updates the management of retired and reclaimed pages to account for the new management. To keep this management simple, when a page is reclaimed, it is eagerly split into one block size.
Following this change, all unused heap pages are unmapped, except for the pages that keep metadata on this heap region. Heap pages are retired and reclaimed in FIFO order (as are FSBs) to have the longest time between using a page and it getting used again and increasing the change that page will be retired and catch any use after free.
FSB: Remove Page Accounting Node
An earlier refactor the FSB introduced the page accounting node as a last used cache for heap owned regions as a potential performance improvement. However, the performance of just this piece was not tested independently.
When testing independently on physical HW, it was not observed to add any performance benefit, so it is dropped.
How This Was Tested
Tested on Q35 and ArmVirt as well as physical Intel and physical Arm. Performance testing was done on the two physical machines and no significant performance delta was observed with these changes (as an entire changeset).
Integration Instructions
Use-after-free has a higher chance of getting caught, so page faults might be seen after these changes. Drivers accessing free memory need to be fixed.